package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.HttpResponseEnum;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.seckill.TbSeckillGoods;
import com.dongdongshop.pojo.seckill.TbSeckillOrder;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.service.SeckillGoodsServiceI;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("seckillController")
public class SeckillController {

    private final Logger logger= LoggerFactory.getLogger(SeckillController.class);

    @Reference
    private SeckillGoodsServiceI seckillGoodsServiceI;

    //跳转到商品秒杀页面
    @RequestMapping("showSeckill")
    public String showSeckill(Model model){
        List<TbSeckillGoods> seckillGoodsList=seckillGoodsServiceI.findSeckillGoodsList();
        model.addAttribute("seckillGoodsList",seckillGoodsList);
        return "seckill-index";
    }

    //查询单个商品 并跳转到详情页面 根据id
    @RequestMapping("getGoodsById/{id}")
    public String getGoodsById(@PathVariable("id")Long id,Model model){
        TbSeckillGoods seckillGoods=seckillGoodsServiceI.getGoodsById(id);
        model.addAttribute("seckillGoods",seckillGoods);
        return "seckill-item";
    }

    //提交订单
    @RequestMapping("submitOrder")
    @ResponseBody
    public Result submitOrder(Long id){
      //登录之后才能抢购
        TbUser user=(TbUser) SecurityUtils.getSubject().getPrincipal();
        if (null == user){
            return Result.jsonResponse(HttpResponseEnum.UNATUTH).setData("请先登录");
        }
       try {
           seckillGoodsServiceI.submitOrder(id,user.getId());
       }catch (Exception e){
           logger.error("系统错误",e);
           return Result.jsonResponse(HttpResponseEnum.FAIL);
       }
       return Result.jsonResponse(HttpResponseEnum.OK);
    }
   //支付获取订单id
    @RequestMapping("getOrderId")
    @ResponseBody
    public Result getOrderId(){
        try {
            TbUser user=(TbUser) SecurityUtils.getSubject().getPrincipal();
            //根据用户查询商品id
            TbSeckillOrder seckillOrder=seckillGoodsServiceI.getOrderId(user.getId());
            Map<String,String> map=new HashMap<>();
            if (null == seckillOrder){
                return Result.jsonResponse(HttpResponseEnum.FAIL);
            }else {
                map.put("out_trade_no",seckillOrder.getId().toString());
                map.put("total_amount",seckillOrder.getMoney().toString());
                map.put("userId",seckillOrder.getUserId());
                return Result.jsonResponse(HttpResponseEnum.OK).setData(map);
            }
        }catch (Exception e){
            logger.error("系统错误",e);
            return Result.jsonResponse(HttpResponseEnum.FAIL);
        }
    }

}
